package com.skl.base.viewmodel;

import androidx.lifecycle.ViewModel;

import com.skl.base.model.SuperBaseModel;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

/**
 * 应用模块: viewModel
 * 类描述: 管理 v M
 */
public abstract class MvvmBaseViewModel<V, M extends SuperBaseModel> extends ViewModel implements IMvvmBaseViewModel<V> {

    private Reference<V> mUiReference;

    protected M model;

    @Override
    public void attachUI(V view) {
        mUiReference = new WeakReference<>(view);
    }

    @Override
    public V getPage() {
        return mUiReference != null ? mUiReference.get() : null;
    }

    @Override
    public boolean isAttachUI() {
        return null != mUiReference && null != mUiReference.get();
    }

    @Override
    public void detachUI() {

        if (mUiReference != null) {
            mUiReference.clear();
            mUiReference = null;
        }

        if (model!=null) {
            model.cancelDisposable();
        }
    }

    private void loadData(){

    }

    protected abstract void initModel();
}
